﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Ue.Qcdz.Domain.TreeViews;
using Ue.Framework.Collections;
using Ue.Infrastructure.RepositoryFramework;

namespace Ue.Qcdz.Repositories.TreeViews
{
    public class TreeNodeProxy : TreeNode
    {
        private IRepository<TreeNode> repository;
        private object parentKey;
        private bool isLoadedChildNodes;
        private bool isLoadedParent;

        private object lockObject = new object();

        public TreeNodeProxy(object key, object parentKey, int displayOrder,DateTime createOn)
            : base(key,null, null, null)
        {
            this.CreateOn = createOn;
            this.DisplayOrder = displayOrder;
            this.parentKey = parentKey;
            this.isLoadedChildNodes = false;
            this.isLoadedParent = false;

            this.repository = new TreeNodeRepository();
        }

        public new TreeView TreeView
        {
            get { return base.TreeView; }
            set { base.TreeView = value; }
        }

        public new ITreeNodeTag Tag
        {
            get { return base.Tag; }
            set { base.Tag = value; }
        }

        public override TreeNode Parent
        {
            get 
            {
                if (base.Parent == null && !this.isLoadedParent)
                {
                    lock (lockObject)
                    {
                        if (base.Parent == null && !this.isLoadedParent)
                        {
                            base.Parent = this.repository[this.parentKey];
                            if (base.Parent is TreeNodeProxy)
                            {
                                ((TreeNodeProxy)this.Parent).TreeView = this.TreeView;
                            }
                        }
                    }
                }

                return base.Parent; 
            }
            //internal set 
            //{ 
            //    base.Parent = value;
            //    this.isLoadedParent = true;
            //}
        }

        public Type TagType { get; set; }

        //public Type TagRepositoryInterfaceType { get; set; } 

        public override IEnumerable<TreeNode> ChildNodes
        {
            get
            {
                if (!isLoadedChildNodes)
                {
                    lock (lockObject)
                    {
                        if (!isLoadedChildNodes)
                        {
                            TreeNodeDataQuery nodeDataQuery = new TreeNodeDataQuery(this.TreeView, 1, 9999);
                            nodeDataQuery.Parent = this;

                            this.repository.FindBy(nodeDataQuery).DataObjects.Foreach(item =>
                            {
                                this.TreeView.AddNode(item);
                            });
                        }
                    }
                }

                return base.ChildNodes;
            }
        }
    }
}
